package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/28 11:46
 * @Version 1.0
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 商品检索
     * http://list.gmall.com/list.html?category3Id=61
     * http://list.gmall.com/list.html?keyword=小米手机&trademark=3:华为
     * @return
     */
    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model){
        //  后台存储： searchParam urlParam trademarkParam propsParamList orderMap
        //  远程调用获取数据
        Result<Map> result = listFeignClient.list(searchParam);
        //  获取用户检索条件进行拼接！
        String urlParam = this.makeUrlParam(searchParam);
        //  品牌面包屑： trademarkParam
        String trademarkParam = this.makeTrademarkParam(searchParam.getTrademark());
        //  平台属性面包屑：propsParamList
        List<SearchAttr> propsParamList = this.makePropsParamList(searchParam.getProps());
        //  指定排序规则：orderMap
        Map<String,Object> orderMap = this.makeOrderMap(searchParam.getOrder());

        //  result.getData(); = SearchResponseVo
        //  trademarkList attrsList goodsList pageNo totalPages -- 属于返回SearchResponseVo属性 因为map 与实体类能够互换！
        model.addAllAttributes(result.getData());
        model.addAttribute("searchParam",searchParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("orderMap",orderMap);
        //  返回检索页面
        return "list/index";
    }

    /**
     * 排序规则
     * @param order
     * @return
     */
    private Map<String, Object> makeOrderMap(String order) {
        //  创建map 对象
        Map<String, Object> map = new HashMap<>();
        //  先判断
        if (!StringUtils.isEmpty(order)){
            //  1:desc 1:asc  2:desc 2:asc
            //  分割获取数据
            String[] split = order.split(":");
            //  判断
            if (null!=split && split.length == 2){
                //  设置按照那个字段进行排序
                map.put("type",split[0]);
                //  设置排序规则
                map.put("sort",split[1]);
            }
        }else {
            //  设置按照那个字段进行排序
            map.put("type","1");
            //  设置排序规则
            map.put("sort","desc");
        }
        //  返回排序规则
        return map;
    }

    /**
     * 平台属性面包屑
     * @param props
     * @return
     */
    private List<SearchAttr> makePropsParamList(String[] props) {
        //  创建一个集合对象
        List<SearchAttr> list = new ArrayList<>();
        //  判断
        if (null != props && props.length>0){
            //  循环遍历 &props=12534:16g:内存&props=12562:暗夜黑:颜色
            for (String prop : props) {
                //  分割
                String[] split = prop.split(":");
                if (null!=split && split.length==3){
                    //  创建对象
                    SearchAttr searchAttr = new SearchAttr();
                    //  给属性赋值
                    searchAttr.setAttrId(Long.parseLong(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    //  将对象添加到集合中
                    list.add(searchAttr);
                }
            }
        }
        return list;
    }

    /**
     * 品牌面包屑
     * @param trademark trademark=3:华为
     * @return
     */
    private String makeTrademarkParam(String trademark) {
        //  判断
        if (!StringUtils.isEmpty(trademark)){
            //  分割获取数据
            String[] split = trademark.split(":");
            if (null != split && split.length ==2){
                return "品牌:"+split[1];
            }
        }
        return null;
    }

    /**
     * 拼接检索条件
     * @param searchParam 记录当前用户所有的检索条件
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        //  创建一个字符串拼接对象
        StringBuffer stringBuffer = new StringBuffer();
        //  用户可能是根据三级分类Id 检索
        //  http://list.gmall.com/list.html?category3Id=61
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            stringBuffer.append("category3Id=").append(searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            stringBuffer.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            stringBuffer.append("category1Id=").append(searchParam.getCategory1Id());
        }

        //  用户可能根据关键词检索 http://list.gmall.com/list.html?keyword=手机
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            stringBuffer.append("keyword=").append(searchParam.getKeyword());
        }
        //  还有可能根据品牌过滤 //  http://list.gmall.com/list.html?keyword=手机&trademark=1:小米
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            if (stringBuffer.length()>0){
                stringBuffer.append("&trademark=").append(trademark);
            }
        }

        //  还有可能根据平台属性值过滤 --- 传递的时候，应该是一个数组;
        //  http://sph-list.atguigu.cn/list.html?category3Id=61&props=12534:16g:内存&props=12562:暗夜黑:颜色
        String[] props = searchParam.getProps();
        if (null != props && props.length>0){
            //  循环遍历
            for (String prop : props) {
                if (stringBuffer.length()>0){
                    stringBuffer.append("&props=").append(prop);
                }
            }
        }
        //  返回最终的拼接条件
        return "list.html?"+stringBuffer.toString();
    }
}
